// import { useModel } from '@umijs/max';
import React, { useEffect, useState } from 'react';
import { ProList } from '@ant-design/pro-components';
import './index.less';
import {Conversations} from "@ant-design/x";

interface ChatSessionListProps {
  sessions: ChatSession[];
  onChange: (agent: string) => void;
  sessionId?: string;
}

export type ChatSession = {
  id: string;
  name: string;
};

const ChatSessionList: React.FC = ({ sessions, onChange, sessionId }: ChatSessionListProps) => {
  //const { initialState } = useModel('@@initialState');
  const [selectedRowKeys, setSelectedRowKeys] = useState<string>(sessionId);
  console.log('session id passed into the ChatSessionList:', sessionId)

  const handleRowClick = (record: string) => {
    console.log('clicked on session row:', record);
    // 你可以在这里添加自定义逻辑，比如选中行
    setSelectedRowKeys(record);
    onChange(record);
  };

  useEffect(() => {
    if (sessionId) {
      let session = sessions.find((item) => item.id == sessionId);
      if (session) {
        console.log('find session by session id', session, sessionId);
        onChange(session.id);
        setSelectedRowKeys(session.id);
        return;
      } else {
        console.log('session not found by session id', sessionId);
      }
    } else {
      console.log('session id is not passed into the ChatSessionList')
      setSelectedRowKeys(null);
    }
  }, [sessionId]);

  return (
    <Conversations<ChatSession>
      className={'chat-session-list'}
      items={sessions}
      onActiveChange={handleRowClick}
      activeKey={selectedRowKeys}
    />
  );
};

export default ChatSessionList;
